package com.example.myxmpp.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.myxmpp.dbhelper.ContactOpenHelper;

public class ContactsProvider extends ContentProvider {

    //得到一个类的完整路径
    //主机地址的常量－－〉当前类的完整治路径
    public  static  final String authorities = ContactsProvider.class.getCanonicalName();
    //地址匹配对像
    static UriMatcher mUriMatcher;

    //对应联系人表的一个ＵＲＩ常量
    public  static Uri  URI_CONTACT = Uri.parse("content://"+authorities+"/contact");

    public  static final int CONTACT = 1;

    static{
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        //添加一个匹配的规则
        mUriMatcher.addURI(authorities,"/contact",CONTACT);
    }

    private ContactOpenHelper mHelper;
    private Uri uri1;

    @Override
    public boolean onCreate() {
        mHelper = new ContactOpenHelper(getContext());
        if (mHelper != null){
            return  true;
        }
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        Cursor cursor = null;
        int code = mUriMatcher.match(uri);
        switch (code){
            case CONTACT:
                SQLiteDatabase db = mHelper.getReadableDatabase();
                cursor = db.query(ContactOpenHelper.T_CONTACT,projection,selection,selectionArgs,null,null,sortOrder);
                Log.i("query","==========query ContactsProvider=======");
                break;

            default:break;
        }
        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        //数据是存在sqlite
        int code = mUriMatcher.match(uri);
        switch (code){
            case CONTACT:
                SQLiteDatabase db = mHelper.getWritableDatabase();
                long id = db.insert(ContactOpenHelper.T_CONTACT,"",values);
                if (id != -1)
                {
                    Log.i("insert","==========insert ContactsProvider=======");
                    //拼接最新的uri   content://com.包名.provider.ContactsProvider/contact/id
                    uri1 = ContentUris.withAppendedId(uri,id);
                    //通知ContentObserver数据改变了
                    getContext().getContentResolver().notifyChange(ContactsProvider.URI_CONTACT,null);//为空所有可以接收到
                }
                break;

                default:break;
        }
        return uri1;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        int code = mUriMatcher.match(uri);
        int deleteCount =0;
        switch (code){
            case CONTACT:
                SQLiteDatabase db = mHelper.getWritableDatabase();
                deleteCount = db.delete(ContactOpenHelper.T_CONTACT,selection,selectionArgs);
                if(deleteCount > 0){
                    Log.i("delete","==========delete ContactsProvider=======");
                    //通知ContentObserver数据改变了
                    getContext().getContentResolver().notifyChange(ContactsProvider.URI_CONTACT,null);//为空所有可以接收到
                }
                break;

            default:break;
        }
        return deleteCount;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        int code = mUriMatcher.match(uri);
        int updateCount =0;
        switch (code){
            case CONTACT:
                SQLiteDatabase db = mHelper.getWritableDatabase();
                updateCount = db.update(ContactOpenHelper.T_CONTACT,values,selection,selectionArgs);
                if (updateCount >0){
                    Log.i("update","==========update ContactsProvider=======");
                    //通知ContentObserver数据改变了
                    getContext().getContentResolver().notifyChange(ContactsProvider.URI_CONTACT,null);//为空所有可以接收到
                }
                break;

            default:break;
        }
        return updateCount;
    }
}
